/**
 * Created by Administrator on 2015/2/4.
 */

var utils = require('../routes/utils');
var service = require('../routes/service');
var logger = require('../routes/logger');
var Fiber = require('fibers');
var querystring = require('querystring');
var xml2js = require('xml2js');
var assert = require('assert');


var request = function (command, from) {
    var data = {command: command, from: from};
    var url = testURL + querystring.stringify(data);
    var xmlResult = utils.syncHttpGet(url);
    var parser = new xml2js.Parser({explicitArray: false, async: false});
    var jsonResult = null;
    parser.parseString(xmlResult, function (err, result) {
        jsonResult = result.xml;
    });
    logger.info("json result", jsonResult['Content'])
    return jsonResult;
}
var testURL = "http://127.0.0.1:48080/ghost/test?";


var flowTest = function () {
    Fiber(function () {
        var creater = getUser(true);
        var result = request("新游戏%钻石%牙齿", creater);
        assert(result['Content'].indexOf("你的昵称") > 0);
        result = request("昵称%法官", creater);
        assert(result['Content'].indexOf("你好") > 0);
        result = request("新游戏%钻石%牙齿", creater);
        var gameId = result['Content'].substring(result['Content'].length - 4);
        logger.info("gameId", gameId);
        assert(result['Content'].indexOf("游戏号") > 0);
        result = request("状态%" + gameId, creater);
        assert(result['Content'].indexOf("无用户加入") > 0);
        result = request("角色%" + gameId, creater);
        assert(result['Content'].indexOf("没有角色") > 0);
        result = request("开始%" + gameId, creater);
        assert(result['Content'].indexOf("人数请控制") > 0);
        result = request("加入%" + gameId, creater);
        assert(result['Content'].indexOf("不能加入游戏") > 0);
        var gamers = [];
        for (var count = 0; count < 10; count++) {
            var gamer = getUser(false);
            gamers.push(gamer);
            result = request("加入%" + gameId, gamer);
            assert(result['Content'].indexOf("你的昵称") > 0);
            result = request("昵称%" + gamer, gamer);
            assert(result['Content'].indexOf("你好") > 0);
            result = request("加入%" + gameId, gamer);
            assert(result['Content'].indexOf("成功") > 0);
        }
        result = request("角色%" + gameId, gamers[0]);
        assert(result['Content'].indexOf("无法获取角色") > 0);

        result = request("角色%" + gameId, creater);
        assert(result['Content'].indexOf("没有角色") > 0);
        result = request("状态%" + gameId, creater);
        assert(result['Content'].indexOf("已加入用户") > 0);
        result = request("开始%" + gameId, creater);
        assert(result['Content'].indexOf("开始") > 0);
        result = request("角色%" + gameId, creater);
        assert(result['Content'].indexOf("没有角色") > 0);
        var gamer = getUser(false);
        request("昵称%" + gamer, gamer);
        result = request("加入%" + gameId, gamer);
        assert(result['Content'].indexOf("不允许加入") > 0);
        result = request("角色%" + gameId, gamer);
        assert(result['Content'].indexOf("未加入该游戏") > 0);
        result = request("状态%" + gameId, creater);
        assert(result['Content'].indexOf("进行中") > 0);
        result = request("结束%" + gameId, gamer);
        assert(result['Content'].indexOf("才能执行") > 0);
        for (var i = 0; i < gamers.length; i++) {
            var gamer = gamers[i];
            result = request("角色%" + gameId, gamer);
            logger.info("角色:" + gamer, result['Content']);
        }
        result = request("结束%" + gameId, creater);
        assert(result['Content'].indexOf("已结束") > 0);
        result = request("状态%" + gameId, creater);
        assert(result['Content'].indexOf("房间状态:已结束") == 0);
        result = request("结束%" + gameId, creater);
        //assert(result['Content'].indexOf("已结束") > 0);
    }).run();
}


var getUser = function (creater) {
    var base = creater ? "creater" : "gamer";
    var result = base + new Date().getTime();
    return result;
}


flowTest();


